package com.asset.salesforce.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.domain.Action;
import com.asset.salesforce.domain.ActionColumnPredefinedValue;
import com.asset.salesforce.domain.ActionField;
import com.asset.salesforce.domain.ActionFieldValue;
import com.asset.salesforce.domain.ActionSelectedValue;
import com.asset.salesforce.domain.ActionTextValue;
import com.asset.salesforce.domain.Employee;
//import com.asset.salesforce.domain.ViewType;
import com.asset.salesforce.form.ComonValidationTypesForm;
import com.asset.salesforce.service.ActionColumnPredefinedValueService;
import com.asset.salesforce.service.ActionFieldService;
import com.asset.salesforce.service.ActionFieldValueService;
import com.asset.salesforce.service.ActionSelectedValueService;
import com.asset.salesforce.service.ActionService;
import com.asset.salesforce.service.ActionTextValueService;
import com.asset.salesforce.service.ActionTypeService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.JoinRequestService;
//import com.asset.salesforce.service.ViewTypeService;
import com.asset.salesforce.domain.ActionType;
import com.asset.salesforce.enums.VIEWTYPE;
@Controller
public class ActionController {

//	@Autowired
//	private ViewTypeService viewTypeService;
	@Autowired
	private ActionService actionService;
	@Autowired
	private ActionTypeService actionTypeService;
	@Autowired
	private ActionFieldService actionFieldService;
	@Autowired
	private ActionFieldValueService actionFieldValueService;
	@Autowired
	private ActionSelectedValueService actionSelectedValueService;
	@Autowired
	private ActionTextValueService actionTextValueService;
	@Autowired
	private ActionColumnPredefinedValueService actionColumnPredefinedValueService;
	@Autowired
	private JoinRequestService joinRequestService;
    @Autowired
    private CalendarService calendarService;
	@RequestMapping(value="/new-action-type",method=RequestMethod.GET)
	public String newActionType(Model model,HttpSession session,HttpServletRequest request) {
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		model.addAttribute("userDetails", userDetails);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "new-action-type";
	}
	
	@RequestMapping(value="/new-action",method=RequestMethod.GET)
	public String newAction(Model model,HttpSession session,HttpServletRequest request, ModelMap modelMap) {
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Integer typeId = Integer.parseInt(request.getParameter("typeId").toString());
		Integer parentId = Integer.parseInt(request.getParameter("parentId").toString());
		List<ActionField> actionFields = actionFieldService.getFieldsByTypeId(typeId);
		System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + actionFields.size());
		model.addAttribute("actionFields", actionFields);
		ComonValidationTypesForm  comonValidationTypesForm = new ComonValidationTypesForm();
		modelMap.addAttribute("comonValidationTypesForm", comonValidationTypesForm);
		model.addAttribute("typeId", typeId);
		model.addAttribute("parentId", parentId);
		model.addAttribute("userDetails", userDetails);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "new-action";
	}
	
	
	@RequestMapping(value="/new-action-type" ,method=RequestMethod.POST)
	public String postNewActionType(HttpSession session,HttpServletRequest request,
			@RequestParam(value="fieldsNum",required=false)Integer fieldsNum,
			@RequestParam(value="actionTypeName",required=false)String actionTypeName){
			Employee userDetails=(Employee)session.getAttribute("userDetails");
			ActionType actionType = new ActionType();
			actionType.setName(actionTypeName);
			actionType.setOwner(userDetails);
			actionTypeService.addActionType(actionType);
			
			for(int i= 1 ; i <= fieldsNum;i++){
				  if(request.getParameter("fieldName"+i)!= null){
					  ActionField actionField = new ActionField();
					  String fieldName = request.getParameter("fieldName"+i);
					  String dataType = request.getParameter("actionDataType"+i);
					  String validationType = request.getParameter("actionValidationType"+i);
					  String viewType = request.getParameter("actionViewType"+i);
					  actionField.setActionType(actionType);
					  actionField.setName(fieldName);
					  actionField.setValidationType(validationType);
					  actionField.setDataType(dataType);
					  actionField.setRender(viewType);
					  actionFieldService.addActionField(actionField);
					  if(viewType.equalsIgnoreCase("select")||viewType.equalsIgnoreCase("checkbox") || viewType.equalsIgnoreCase("radio")){
						  Integer fields= Integer.parseInt(request.getParameter("field"+i+"ValuesCount"));
						  for(int j= 1 ;j<=fields ;j++){
							  ActionColumnPredefinedValue actionColumnPredefinedValue =new ActionColumnPredefinedValue();
							  actionColumnPredefinedValue.setActionFieldId(actionField);
							  actionColumnPredefinedValue.setValue(request.getParameter("field"+i+"value"+j));
							  actionColumnPredefinedValueService.addActionColumnPredefinedValue(actionColumnPredefinedValue);
						  }
					  }
				  }
			}
			return  "redirect:/s/leads.html";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/new-action",method=RequestMethod.POST)
	public String postNewAction(HttpSession session,HttpServletRequest request,

			@RequestParam(value="parentId",required=false)Integer parentId,
			@RequestParam(value="typeId",required=false)Integer typeId){

        Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
//		ViewType viewType = viewTypeService.getViewTypeById(1);
		
		Action action = new Action();
		ActionType actionType1 = new ActionType();
		ActionType actionType = actionTypeService.getActionTypeById(actionType1, typeId);
		action.setActionType(actionType);
		action.setName("actionName");
		action.setOwner(userDetails);
		
//		action.setParent(parentId);
		action.setViewType(VIEWTYPE.Lead.getName());
		actionService.addAction(action);
		ActionField actionField = new ActionField();
		  Map<String, String[]> map = request.getParameterMap();	 
	       
	        Set set = map.entrySet();
	        Iterator it = set.iterator();
	        //action.setActionType(actionType);
	        action.setOwner(userDetails);
	        while (it.hasNext()) {
	        	
	            Map.Entry<String, String[]> entry = (Entry<String, String[]>) it.next();
	            String paramName = entry.getKey();
	            if(!paramName.equalsIgnoreCase("typeId") && !paramName.equalsIgnoreCase("parentId") &&
	            		!paramName.equalsIgnoreCase("notEmptyString") && !paramName.equalsIgnoreCase("optionalString") && !paramName.equalsIgnoreCase("scheduledDate")){
	            System.out.println("<tr><td>" + paramName + "</td><td>");
	            ActionField actionField1 = actionFieldService.getActionFieldById(actionField, Integer.parseInt(paramName));
	            String fieldRender = actionField1.getRender();
	            String fieldValueType;
	            ActionFieldValue actionFieldValue =  new ActionFieldValue();
	            actionFieldValue.setActionField(actionField1);
	            if(fieldRender.equalsIgnoreCase("text") || fieldRender.equalsIgnoreCase("textarea")){
	            	fieldValueType = "0";
	            }
	            else{
	            	fieldValueType = "1";
	            }
            	System.out.println( fieldRender + " nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn " + fieldValueType );
	            actionFieldValue.setType(fieldValueType);
	            //actionFieldValueService.addActionFieldValue(actionFieldValue);
	            actionFieldValue.setAction(action);
	            
	            ActionTextValue actionTextValue = new ActionTextValue();
	            ActionColumnPredefinedValue actionColumnPredefinedValue = new ActionColumnPredefinedValue();
	            ActionSelectedValue actionSelectedValue = new ActionSelectedValue();
	            actionFieldValueService.addActionFieldValue(actionFieldValue);
	            
	            String[] paramValues = entry.getValue();
	            if (paramValues.length == 1) {
	                String paramValue = paramValues[0];
	                if (paramValue.length() == 0)
	                	System.out.println("<b>No Value</b>");
	                else
	                {
	                	System.out.println(paramValue);
	                	if(fieldValueType == "0"){
	                		actionTextValue.setValue(paramValue);
	                		actionTextValueService.addActionTextValue(actionTextValue);
	                		actionSelectedValue.setActionFieldValue(actionFieldValue);
	                		actionSelectedValue.setValueId(actionTextValue.getId());
	                		actionSelectedValueService.addActionSelectedValue(actionSelectedValue);
	    	            }
	                	else if(fieldValueType == "1"){
	                		ActionColumnPredefinedValue actionColumnPredefinedValue2 =
	                				actionColumnPredefinedValueService.getActionColumnPredefinedValueById(actionColumnPredefinedValue, Integer.parseInt(paramValue));

	                		actionSelectedValue.setActionFieldValue(actionFieldValue);
	                		actionSelectedValue.setValueId(actionColumnPredefinedValue2.getId());
	                		actionSelectedValueService.addActionSelectedValue(actionSelectedValue);
	    	            }
	                }
	            } else {
	            	System.out.println("<ul>");
	                for (int i = 0; i < paramValues.length; i++) {
	                	System.out.println("<li>" + paramValues[i] + "</li>");
	                }
	            }
	            }
	            else{
	            	System.out.println("777777777777777777777777777777777777777777777777777777777");
	            }
	        }
	        return "redirect:/s/leads.html";
}
}
 